/*
 * Copyright 2010 WANdisco, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package com.googlecode.svnj.reports;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLStreamException;

import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.DavResource;
import org.apache.jackrabbit.webdav.WebdavRequest;
import org.apache.jackrabbit.webdav.WebdavResponse;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.googlecode.svnj.XMLUtil;

public abstract class ReportRequest {

    public static final String TYPE_UPDATE = "update";
    public static final String TYPE_GET_LOCATIONS = "get-locations";
    public static final String TYPE_LOCATION_SEGMENTS = "get-location-segments";
    public static final String TYPE_LOG = "log";
    public static final String TYPE_FILE_REVS = "file-revs";
    public static final String TYPE_MERGEINFO = "mergeinfo";
    
    public abstract String getReportType();
    protected abstract void parse(Node root);
    /**
     * Run the report
     * @param req
     * @param rsp
     * @param resource
     * @throws DavException
     * @throws IOException
     * @throws XMLStreamException
     */
    public abstract void report(WebdavRequest req, WebdavResponse rsp, DavResource resource)
    throws DavException, IOException, XMLStreamException;

    
    public static ReportRequest parse(Reader rdr) throws IOException {
        Document doc = null;
        doc = XMLUtil.parseDoc(rdr);
        Node root = doc.getFirstChild();
        ReportRequest rr = null;
        String name = root.getLocalName();
        if ("update-report".equals(name)) {
            rr = new UpdateReport();
        } else if ("get-locations".equals(name)) {
            rr = new GetLocationsReport();
        } else if ("log-report".equals(name)) {
            rr = new LogReport();
        } else if ("file-revs-report".equals(name)) {
            rr = new FileRevsReport();
        } else if ("mergeinfo-report".equals(name)) {
            rr = new MergeinfoReport();
        } else if ("get-location-segments".equals(name)) {
            rr = new GetLocationSegmentsReport();
        } else {
            throw new RuntimeException(
                    "Unimplemented report '" + root.getNodeName() + "'"
                    );
        }
        rr.parse(root);
        return rr;
        
    }
     
    public static ReportRequest parse(String xml) throws IOException {
        return parse(new StringReader(xml));
    }
}
